From: <Saved by Windows Internet Explorer 7>
Subject: Thirty-seven Reasons I Love Ruby
Date: Sat, 3 Nov 2007 17:24:11 +0100
MIME-Version: 1.0
Content-Type: text/html;
	charset="windows-1251"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://rubyhacker.com/ruby37.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Thirty-seven Reasons I Love Ruby</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Dwindows-1251">
<SCRIPT>=0A=
   <!--=0A=
    function gCounter(tag)=0A=
   {=0A=
     document.write('<IMG SRC=3D"http://hypermetrics.com/cgi-bin/');=0A=
     document.write('imagecounter?df=3Dhypermetrics_');=0A=
     document.write(tag);=0A=
     document.write('.dat|ft=3D0|pad=3D0|dd=3DD">');=0A=
   }=0A=
   //-->=0A=
   </SCRIPT>

<META content=3D"MSHTML 6.00.6000.16544" name=3DGENERATOR></HEAD>
<BODY>
<TABLE width=3D"100%">
  <TBODY>
  <TR>
    <TD>
      <HR color=3Dred>
    </TD>
    <TD width=3D"70%" bgColor=3Dred><FONT color=3Dwhite size=3D+2>
      <P align=3Dcenter>Thirty-seven Reasons I Love Ruby =
</P></FONT></TD>
    <TD>
      <HR color=3Dred>
    </TD></TR></TBODY></TABLE>
<P>I won't bother giving the history of the Ruby language here. For =
those of you=20
who haven't heard of it, the definitive web site is www.ruby-lang.org, =
and=20
comp.lang.ruby is the newsgroup. For those who have, I present to you =
the=20
reasons I love this (relatively) new language. You may also visit my =
main <A=20
href=3D"http://rubyhacker.com/">Ruby page</A> if you didn't come from =
there.=20
<P></P>
<OL>
  <LI>
  <P><B><FONT color=3Dred>It's object-oriented.</FONT></B> What does =
that mean?=20
  Well, for every ten programmers, there are twelve opinions as to what =
OOP is.=20
  I will leave it your judgment. But for the record, Ruby does offer=20
  encapsulation of data and methods within objects and allows =
inheritance from=20
  one class to another; and it allows polymorphism of objects. Unlike =
some=20
  languages (C++, Perl 5, etc.) Ruby was designed from the beginning to =
be=20
  object-oriented.</P>
  <LI>
  <P><B><FONT color=3Dred>It's a <I>pure</I> OOP language.</FONT> </B>Am =
I being=20
  redundant? I don't think so. By this we mean that everything, =
including=20
  primitive data types such as strings and integers, is represented as =
an=20
  object. There is no need for wrapper classes such as Java has. And in=20
  addition, even constants are treated as objects, so that a method may =
be=20
  invoked with, for example, a numeric constant as a receiver.</P>
  <LI>
  <P><B><FONT color=3Dred>It's a dynamic language.</FONT> </B>For people =
only=20
  familiar with more static languages such as C++ and Java, this is a=20
  significant conceptual leap. It means that methods and variables may =
be added=20
  and redefined at runtime. It obviates the need for such features as =
C's=20
  conditional compilation (<B>#ifdef</B>), and makes possible a =
sophisticated=20
  reflection API. This in turn allows programs to become more =
"self-aware" --=20
  enabling runtime type information, detection of missing methods, hooks =
for=20
  detecting added methods, and so on. Ruby is related to Lisp and =
Smalltalk in=20
  this respect.</P>
  <LI>
  <P><B><FONT color=3Dred>It's an interpreted language.</FONT> </B>This =
is a=20
  complex issue, and deserves several comments. It can be argued that=20
  performance issues make this a negative rather than a positive. To =
this=20
  concern, I reply with these observations: 1. First and foremost: A =
rapid=20
  development cycle is a great benefit, and it is encouraged by the =
interpreted=20
  nature of Ruby. 2. How slow is too slow, anyway? Do some benchmarks =
before you=20
  call it slow. 3. Though some will criticize me, I will say this =
anyway:=20
  Processors are getting faster every year. 4. If you absolutely need =
the speed,=20
  you can write part of your code in C. 5. Finally, in a sense, it is =
all a moot=20
  point, since no language is inherently interpreted. There is no law of =
the=20
  universe that says a Ruby compiler cannot be written. </P>
  <LI>
  <P><B><FONT color=3Dred>It understands regular expressions.</FONT> =
</B>For=20
  years, this was considered the domain of UNIX weenies wielding clumsy =
tools=20
  such as <B>grep </B>and <B>sed</B>, or doing fancy search-and-replace=20
  operations in <B>vi</B>. Perl helped change that, and now Ruby is =
helping,=20
  too. More people than ever recognize the incredible power in the=20
  super-advanced string and text manipulation techniques. Doubters =
should go and=20
  read Jeffrey Friedl's book <I>Mastering Regular Expressions</I>. So =
should=20
  non-doubters.</P>
  <LI>
  <P><B><FONT color=3Dred>It's multi-platform.</FONT> </B>It runs on =
Linux and=20
  other UNIX variants, the various Windows platforms, BeOS, and even =
MS-DOS. If=20
  my memory serves me, there's an Amiga version.</P>
  <LI>
  <P><B><FONT color=3Dred>It's derivative.</FONT> </B>This is a good =
thing?=20
  Outside of the literary world, yes, it is. Isaac Newton said, "If I =
have seen=20
  farther than others, it is because I stood on the shoulders of =
giants." Ruby=20
  certainly has stood on the shoulders of giants. It borrows features =
from=20
  Smalltalk, CLU, Lisp, C, C++, Perl, Kornshell, and others. The =
principles I=20
  see at work are: 1. Don't reinvent the wheel. 2. Don't fix what isn't =
broken.=20
  3. Finally, and especially: Leverage people's existing knowledge. You=20
  understand files and pipes in UNIX? Fine, you can use that knowledge. =
You=20
  spent two years learning all the <B>printf </B>specifiers? Don't =
worry, you=20
  can still use <B>printf</B>. You know Perl's regex handling? Good, =
then you've=20
  almost learned Ruby's.</P>
  <LI>
  <P><B><FONT color=3Dred>It's innovative.</FONT> </B>Is this in =
contradiction to=20
  #7 above? Well, partly; every coin has two sides. Some of Ruby's =
features are=20
  truly innovative, like the very useful concept of the mix-in. Maybe =
some of=20
  these features will be borrowed in turn by future languages. =
(<B>Note:</B> A=20
  reader has pointed out to me that LISP had mix-ins at least as far =
back as=20
  1979. That's purely ignorance on my part; I shall have to find a =
better=20
  example, and make sure of it.)</P>
  <LI>
  <P><B><FONT color=3Dred>It's a Very High-Level Language (VHLL).</FONT> =
</B>This=20
  is subject to debate, because this term is not in widespread use, and =
its=20
  meaning is even more disputable than that of OOP. When I say this, I =
mean that=20
  Ruby can handle complex data structures and complex operations on them =
with=20
  relatively few instructions, in accordance with what some call the =
Principle=20
  of Least Effort.</P>
  <LI>
  <P><B><FONT color=3Dred>It has a smart garbage collector.</FONT> =
</B>Routines=20
  like <B>malloc </B>and <B>free </B>are only last night's bad dream. =
You don't=20
  even have to call destructors. Enough said. </P>
  <LI>
  <P><B><FONT color=3Dred>It's a scripting language.</FONT> </B>Don't =
make the=20
  mistake of thinking it isn't powerful because of this. It's not a toy. =
It's a=20
  full-fledged language that happens to make it easy to do traditional =
scripting=20
  operations like running external programs, examining system resources, =
using=20
  pipes, capturing output, and so on.</P>
  <LI>
  <P><B><FONT color=3Dred>It's versatile.</FONT> </B>It can do the =
things that=20
  Kornshell does well and the things that C does well. You want to write =
a quick=20
  ten-line hack to do a one-time task, or a wrapper for some legacy =
programs?=20
  Fine. You want to write a web server, a CGI, or a chess program? =
Again,=20
  fine.</P>
  <LI>
  <P><B><FONT color=3Dred>It's thread-capable.</FONT> </B>You can write=20
  multi-threaded applications with a simple API. Yes, even on =
MS-DOS.</P>
  <LI>
  <P><B><FONT color=3Dred>It's open-source.</FONT> </B>You want to look =
at the=20
  source code? Go ahead. Want to suggest a patch? Go ahead. You want to =
connect=20
  with a knowledgeable and helpful user community, including the =
language=20
  creator himself? You can.</P>
  <LI>
  <P><B><FONT color=3Dred>It's intuitive.</FONT> </B>The learning curve =
is low,=20
  and once you get over the first hump, you start to "guess" how things =
work=85=20
  and your guesses are often correct. Ruby endeavors to follow the =
Principle of=20
  Least Astonishment (or Surprise).</P>
  <LI>
  <P><B><FONT color=3Dred>It has an exception mechanism.</FONT> </B>Like =
Java and=20
  C++, Ruby understands exceptions. This means less messing with return =
codes,=20
  fewer nested if statements, less spaghetti logic, and better error=20
  handling.</P>
  <LI>
  <P><B><FONT color=3Dred>It has an advanced Array class.</FONT> =
</B>Arrays are=20
  dynamic; you don't have to declare their size at compile-time as in, =
say,=20
  Pascal. You don't have to allocate memory for them as in C, C++, or =
Java.=20
  They're objects, so you don't have to keep up with their length; it's=20
  virtually impossible to "walk off the end" of an array as you might in =
C. Want=20
  to process them by index? By element? Process them backwards? Print =
them?=20
  There are methods for all these. Want to use an array as a set, a =
stack, or a=20
  queue? There are methods for these operations, too. Want to use an =
array as a=20
  lookup table? That's a trick question; you don't have to, since we =
have hashes=20
  for that.</P>
  <LI>
  <P><B><FONT color=3Dred>It's extensible.</FONT> </B>You can write =
external=20
  libraries in Ruby or in C. In addition, you can modify the existing =
classes=20
  and objects at will, on the fly.</P>
  <LI>
  <P><B><FONT color=3Dred>It encourages literate programming.</FONT> =
</B>You can=20
  embed comments in your code which the Ruby documentation tool can =
extract and=20
  manipulate. (Real fans of literate programming may think this is =
pretty=20
  rudimentary.)</P>
  <LI>
  <P><B><FONT color=3Dred>It uses punctuation and capitalization=20
  creatively.</FONT> </B>A method returning a Boolean result (though =
Ruby=20
  doesn't call it that) is typically ended with a question mark, and the =
more=20
  destructive, data-modifying methods are named with an exclamation =
point.=20
  Simple, informative, and intuitive. All constants, including class =
names,=20
  start with capital letters. All object attributes start with an <B>@ =
</B>sign.=20
  This has the pragmatism of the old "Hungarian notation" without the=20
  eye-jarring ugliness.</P>
  <LI>
  <P><B><FONT color=3Dred>Reserved words aren't.</FONT> </B>It's =
perfectly=20
  allowable to use an identifier that is a so-called "reserved word" as =
long as=20
  the parser doesn't perceive an amibiguity. This is a breath of fresh =
air.</P>
  <LI>
  <P><B><FONT color=3Dred>It allows iterators.</FONT> </B>Among other =
things, this=20
  makes it possible to pass blocks of code to your objects in such a way =
that=20
  the block is called for each item in the array, list, tree, or =
whatever. This=20
  is a powerful technique that is worth exploring at great length.</P>
  <LI>
  <P><B><FONT color=3Dred>It has safety and security features. =
</FONT></B>Ruby=20
  borrows Perl's concept of tainting and allows different levels of =
control=20
  (levels of paranoia?) by means of the <B>$SAFE </B>variable. This is=20
  especially good for CGI programs that people will try to subvert in =
order to=20
  crack the web server.</P>
  <LI>
  <P><B><FONT color=3Dred>It has no pointers.</FONT> </B>Like Java, and =
with a=20
  grudging nod to C++, Ruby does not have the concept of a pointer; =
there is no=20
  indirection, no pointer arithmetic, and none of the headaches that go =
with the=20
  syntax and the debugging of pointers. Of course, this means that real=20
  nuts-and-bolts system programming is more difficult, such as accessing =
a=20
  control-status register for a device; but that can always be done in a =
C=20
  library. (Just as C programmers drop into assembly when necessary, =
Ruby=20
  programmers drop into C when they have to!)</P>
  <LI>
  <P><B><FONT color=3Dred>It pays attention to detail.</FONT> =
</B>Synonyms and=20
  aliases abound. You can't remember whether to say <B>size </B>or =
<B>length=20
  </B>for a string or an array? Either one works. For ranges, is it =
<B>begin=20
  </B>and <B>end</B>, or <B>first </B>and <B>last</B>? Take your pick. =
You spell=20
  it <B>indices</B>, and your evil twin spells it <B>indexes</B>? They =
both=20
  work.</P>
  <LI>
  <P><B><FONT color=3Dred>It has a flexible syntax.</FONT> =
</B>Parentheses in=20
  method calls can usually be omitted, as can commas between parameters. =

  Perl-style quotes allow arrays of strings without all the quotation =
marks and=20
  commas. The <B>return </B>keyword can be omitted.</P>
  <LI>
  <P><B><FONT color=3Dred>It has a rich set of libraries.</FONT> =
</B>There is=20
  support for threads, sockets, limited object persistence, CGI =
programs,=20
  server-side executables, DB files, and more. There is some support for =
Tk,=20
  with more on the way.</P>
  <LI>
  <P><B><FONT color=3Dred>It has a debugger.</FONT> </B>In a perfect =
world, we=20
  wouldn't need debuggers. This is not a perfect world.</P>
  <LI>
  <P><B><FONT color=3Dred>It can be used interactively.</FONT> =
</B>Conceivably it=20
  could be used as a sort of "Kornshell squared." (This is the most =
contested=20
  item on this page, and I am forced to concede that Ruby is not really =
good as=20
  a shell. I still maintain, though, that a Ruby-based shell would be a =
good=20
  thing.) </P>
  <LI>
  <P><B><FONT color=3Dred>It is concise.</FONT> </B>There are no =
superfluous=20
  keywords such as Pascal's <B>begin</B>, <B>then </B>after <B>if</B>, =
<B>do=20
  </B>after <B>while</B>. Variables need not be declared, as they do not =
have=20
  types. Return types need not be specified for methods. The return =
keyword is=20
  not needed; a method will return the last evaluated expression. On the =
other=20
  hand... it is not so cryptic as C or Perl. </P>
  <LI>
  <P><B><FONT color=3Dred>It is expression-oriented.</FONT> </B>You can =
easily say=20
  things like <B>x =3D if a&lt;0 then b else c end</B>.</P>
  <LI>
  <P><B><FONT color=3Dred>It is laced with syntax sugar.</FONT> </B>(To =
paraphrase=20
  Mary Poppins: A spoonful of syntax sugar helps the semantic medicine =
go down.)=20
  If you want to iterate over an array <B>x </B>by saying <B>for a in =
x</B>, you=20
  can. If you want to say <B>a +=3D b </B>instead of <B>a =3D a + b</B>, =
you can.=20
  Most operators are really just methods with short, intuitive names and =
a more=20
  convenient syntax.</P>
  <LI>
  <P><B><FONT color=3Dred>It has operator overloading.</FONT> </B>If I =
am not=20
  mistaken, this originated long ago in SNOBOL, but was popularized more =

  recently by C++. It can be overdone or misused, but it can be nice to =
have.=20
  Additionally, Ruby defines the assignment version of an operator=20
  automagically; if you define +, you get <B>+=3D</B> as a bonus.</P>
  <LI>
  <P><B><FONT color=3Dred>It has infinite-precision integer =
arithmetic.</FONT>=20
  </B>Who cares about <B>short</B>, <B>int</B>, <B>long</B>? Just use a=20
  <B>Bignum</B>. Admit it, you always wanted to find the factorial of =
365. Now=20
  you can.</P>
  <LI>
  <P><B><FONT color=3Dred>It has an exponentiation operator.</FONT> =
</B>In the old=20
  days, we used this in BASIC and FORTRAN. But then we learned Pascal =
and C, and=20
  learned how evil this operator was. (We were told we didn't even know =
how the=20
  evaluation was done -- did it use logarithms? Iteration? How efficient =
was=20
  it?) But then, do we really care? If so, we can rewrite it ourselves. =
If not,=20
  Ruby has the good old <B>**</B> operator you loved as a child. Enjoy =
it.</P>
  <LI>
  <P><B><FONT color=3Dred>It has powerful string handling.</FONT> </B>If =
you want=20
  to search, substitute, justify, format, trim, delimit, interpose, or =
tokenize,=20
  you can probably use one of the built-in methods. If not, you can =
build on=20
  them to produce what you need. </P>
  <LI>
  <P><B><FONT color=3Dred>It has few exceptions to its rules.</FONT> =
</B>The=20
  syntax and semantics of Ruby are more self-consistent than most =
languages.=20
  Every language has oddities, and every rule has exceptions; but Ruby =
has fewer=20
  than you might expect.</P>
  <HR color=3Dred>

  <CENTER>
  <SCRIPT>gCounter("ruby37");</SCRIPT>
  hits since October 2000.</CENTER>
  <HR color=3Dred>

  <P align=3Dcenter>
  <TABLE bgColor=3D#ffa8a8>
    <TBODY>
    <TR>
      <TD><FONT color=3Dwhite><A=20
        href=3D"http://rubyhacker.com/index.html"><I>Back</I> to my Ruby =
page</A>=20
        </FONT></TD></TR></TBODY></TABLE></P></LI></OL></BODY></HTML>
